capture log close
clear all
set more off
pause off
# delimit;

local output "";
local log "";
local data "";

/* 
Create Figure A3.2C
*/

use "`data'\collapse_combine_bymale.dta", clear;


/* ******************************* */
/* **** DATA PREP AND RESHAPE **** */
/* ******************************* */

/* Age */
gen age=year-ser_dobyy;
replace age=99 if year==9999;
order ser_dobyy year age;

/* Create <=1937 and >=1943 cohorts */
tab ser_dobyy, m;
gen ser_dobcoh=1937 if ser_dobyy>=1935 & ser_dobyy<=1937;
replace ser_dobcoh=1943 if ser_dobyy>=1943 & ser_dobyy<=1945;
replace ser_dobcoh=9999 if ser_dobcoh==.;
order ser_dobyy ser_dobcoh year age;


/* Sum over month-age and YOB cohort */
collapse (sum) earn10yr_* earn55_*, by(age ser_dobcoh n_male);
sort ser_dobcoh age n_male;

/* Create fractions of cohorts instead of counts (for flow variable, use single event denominator) */
foreach set in earn10yr earn55 {;
foreach stub in `set'_lfexit_flow `set'_lfexit_one `set'_lfexit_one_1 `set'_lfexit_one_rbst `set'_claim_phusold `set'_claim_phusany `set'_claim_mbaold `set'_claim_mbaany {;
	egen total_`stub'=total(`stub'), by(ser_dobcoh n_male);
};
};
foreach set in earn10yr earn55 {;
foreach stub in `set'_lfexit_one `set'_lfexit_one_1 `set'_lfexit_one_rbst `set'_claim_phusold `set'_claim_phusany `set'_claim_mbaold `set'_claim_mbaany {;	
	rename `stub' ct_`stub';
	gen `stub'=ct_`stub'/total_`stub' if ct_`stub'>10;
	replace `stub'=. if ct_`stub'<=10;
};
	rename `set'_lfexit_flow ct_`set'_lfexit_flow;
	gen `set'_lfexit_flow=ct_`set'_lfexit_flow/total_`set'_lfexit_one;
	replace `set'_lfexit_flow=. if ct_`set'_lfexit_flow<=10;
};
drop total_* ct_*;

/* Reshape for plot */
drop if age<50;
egen id=group(age n_male);
reshape wide 
	earn10yr_lfexit_flow earn10yr_lfexit_one earn10yr_lfexit_one_1 earn10yr_lfexit_one_rbst 
	earn10yr_claim_phusold earn10yr_claim_phusany earn10yr_claim_mbaold earn10yr_claim_mbaany
	earn55_lfexit_flow earn55_lfexit_one earn55_lfexit_one_1 earn55_lfexit_one_rbst
	earn55_claim_phusold earn55_claim_phusany earn55_claim_mbaold earn55_claim_mbaany
, i(id) j(ser_dobcoh);
order id age n_male;


/* ***************************************************** */
/* **** PLOT RETIREMENT AND CLAIMING ACROSS COHORTS **** */
/* ***************************************************** */

keep if age>=55 & age<69 ;

local cond_lfexit_one		"age>=55 & age<69" ;

foreach set in earn10yr {;
foreach stub in lfexit_one {;
twoway 	
	connected `set'_`stub'1937 age if n_male==1 , lcolor(blue*.75) lwidth(medthick) lpattern(solid) mcolor(blue*.75) msymbol(S) ||
	connected `set'_`stub'1943 age if n_male==1 & `cond_`stub'', lcolor(blue*.75) lwidth(medthick) lpattern(shortdash) mcolor(blue*.75) msymbol(Sh) ||
	connected `set'_`stub'1937 age if n_male==0 , lcolor(red*.75) lwidth(medthick) lpattern(solid) mcolor(red*.75) msymbol(T) ||
	connected `set'_`stub'1943 age if n_male==0 & `cond_`stub'', lcolor(red*.75) lwidth(medthick) lpattern(shortdash) mcolor(red*.75) msymbol(Th)
	xtitle("Age", size(medsmall)) xsc(r(55 68)) xlab(55(1)68)
	ytitle("Fraction of birth cohort", size(medsmall))
	legend(col(2) row(2) label(1 "Male, FRA 65") label(2 "Male, FRA 66") label(3 "Female, FRA 65") label(4 "Female, FRA 66"))
	graphregion(color(white))
	bgcolor(white)
	ylabel(,nogrid)
	;
	graph export "`output'\\Figure_A32C.png", replace;
};
};
